﻿using System.IO;
using SCResources.DataLayer;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using SCResources.General;

namespace UnitTests
{
    
    
    /// <summary>
    ///This is a test class for SeriesDataTest and is intended
    ///to contain all SeriesDataTest Unit Tests
    ///</summary>
    [TestClass()]
    public class SeriesDataTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Add
        ///</summary>
        [TestMethod()]
        public void AddTest()
        {
            SeriesData target = new SeriesData();
            SerieWrapper serieWrapper = new SerieWrapper();//= new SerieWrapper("testSerie",null,"test","C:\\",new SerieUserInfo(1,1),new SerieAirInfo(1,2,DateTime.Now,7) );
            target.Add(serieWrapper);
            
            Assert.AreEqual(1,target.SeriesDataSet.Series.Count);
            Assert.AreEqual(1, target.SeriesDataSet.AirInfo.Count);
            Assert.AreEqual(1, target.SeriesDataSet.UserInfo.Count);
        }

        /// <summary>
        ///A test for ExportData & write data
        ///</summary>
        [TestMethod()]
        public void ExportDataTest()
        {
            //remove file before writing
            string filename =IO_Settings.GetSettings().SerieListSavePath;
            if(File.Exists(filename)) File.Delete(filename);

            SeriesData target = new SeriesData();
            SerieWrapper serieWrapper = new SerieWrapper();//= new SerieWrapper("testSerie",null,"test","C:\\",new SerieUserInfo(1,1),new SerieAirInfo(1,2,DateTime.Now,7) );
            target.Add(serieWrapper);

            target.ExportData(filename);
            Assert.IsTrue(File.Exists(filename));
        }

        /// <summary>
        ///A test for ImportData & constructors initdata
        ///</summary>
        [TestMethod()]
        public void ImportDataTest()
        {
            //remove file before writing
            string filename = IO_Settings.GetSettings().SerieListSavePath;
            if (File.Exists(filename)) File.Delete(filename);

            SeriesData target = new SeriesData();
            SerieWrapper serieWrapper = new SerieWrapper();//= new SerieWrapper("testSerie",null,"test","C:\\",new SerieUserInfo(1,1),new SerieAirInfo(1,2,DateTime.Now,7) );
            target.Add(serieWrapper);
            target.writeData(); // save default location
            SerieWrapper serieWrapper2 = new SerieWrapper();//= new SerieWrapper("testSerie",null,"test","C:\\",new SerieUserInfo(1,1),new SerieAirInfo(1,2,DateTime.Now,7) );
            target.Add(serieWrapper2);
            target.ExportData("series2.xml");
            
            //new instance
            target = new SeriesData(); //should load default data
            Assert.AreEqual(1,target.SeriesDataSet.Series.Count);
            target.ImportData("series2.xml"); //load external data (2 series)
            Assert.AreEqual(2, target.SeriesDataSet.Series.Count);
            
        }

        /// <summary>
        ///A test for Get
        ///</summary>
        [TestMethod()]
        public void GetTest()
        {
            //todo: retest /rewrite Get
            SeriesData target = new SeriesData();
            //first add a serie
            SerieWrapper serieWrapper = new SerieWrapper();//= new SerieWrapper("testSerie",null,"test","C:\\",new SerieUserInfo(1,1),new SerieAirInfo(1,2,DateTime.Now,7) );
            target.Add(serieWrapper);


            int id = 1; 
            SerieWrapper actual;

            actual = target.Get(id);

            int compare = serieWrapper.CompareTo(actual);
            //actual.Title = "veranderde titel";
            //Assert.IsNotNull(actual);

            Assert.AreEqual(4, compare);
        }

        /// <summary>
        ///A test for Get
        ///</summary>
        [TestMethod()]
        public void RemoveTest()
        {
            SeriesData target = new SeriesData();
            //first add a serie
            SerieWrapper serieWrapper = new SerieWrapper();//= new SerieWrapper("testSerie",null,"test","C:\\",new SerieUserInfo(1,1),new SerieAirInfo(1,2,DateTime.Now,7) );
            target.Add(serieWrapper);
            //remove
            target.Remove(1);
            //all tables should be empty
            Assert.AreEqual(0, target.SeriesDataSet.Series.Count);
            Assert.AreEqual(0, target.SeriesDataSet.UserInfo.Count);
            Assert.AreEqual(0, target.SeriesDataSet.AirInfo.Count);

        }

        /// <summary>
        ///A test for Get
        ///</summary>
        [TestMethod()]
        public void UpdateTest()
        {
            SeriesData target = new SeriesData();
            SerieWrapper serieWrapper = new SerieWrapper();//= new SerieWrapper("testSerie",null,"test","C:\\",new SerieUserInfo(1,1),new SerieAirInfo(1,2,DateTime.Now,7) );
            target.Add(serieWrapper);

            serieWrapper.Title = "veranderd"; //change only title works
            serieWrapper.AirInfo = null;
            target.Update(serieWrapper);

            SerieWrapper actual = target.Get(1);
            int compare = serieWrapper.CompareTo(actual);
            Assert.AreEqual(102, compare); //equal 4, title only
        }
    }
}
